import request from '../request'
import type { IApiResponse } from '@/types/api'
import type { 
  Organization,
  CreateOrganizationParams,
  UpdateOrganizationParams
} from '@/types/organization'

interface ListParams {
  name?: string
  code?: string
  parentId?: number | null
  type?: number
}

export const organizationApi = {
  // 获取组织列表
  getList: (params?: ListParams) => {
    return request<IApiResponse<Organization[]>>({
      url: '/v1/departments',
      method: 'get',
      params: {
        ...params,
        parentId: params?.parentId || null
      }
    })
  },

  // 创建组织
  create: (data: CreateOrganizationParams) => {
    return request<IApiResponse<Organization>>({
      url: '/v1/departments',
      method: 'post',
      data
    })
  },

  // 更新组织
  update: (data: UpdateOrganizationParams) => {
    return request<IApiResponse<Organization>>({
      url: `/v1/departments/${data.id}`,
      method: 'put',
      data
    })
  },

  // 删除组织
  delete: (id: number) => {
    return request<IApiResponse<void>>({
      url: `/v1/departments/${id}`,
      method: 'delete'
    })
  },

  // 获取组织树形结构
  getTree: () => {
    return request<IApiResponse<Organization[]>>({
      url: '/v1/departments/tree',
      method: 'get'
    })
  },

  // 批量删除
  batchDelete: (ids: number[]) => {
    return request<IApiResponse<void>>({
      url: '/v1/organizations/batch',
      method: 'delete',
      data: { ids }
    })
  },

  // 获取组织详情
  getById: (id: number) => {
    return request<IApiResponse<Organization>>({
      url: `/v1/departments/${id}`,
      method: 'get'
    })
  }
} 